package org.tjc.net.manager.config.conf.parser;

import java.io.*;

/**
 * @Author: tjc
 * @description
 * @Date Created in 21:15 2020-12-09
 */
public class ConfParser {

    public static final char EOF = (char) -1;

    private final String confPath;

    private PushbackInputStream inputStream;

    private int line;
    private int offset;

    public ConfParser(String confPath) {
        this.confPath = confPath;
        try {
            inputStream = new PushbackInputStream(new FileInputStream(new File(confPath)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        line = 1;
        offset = 0;
    }

    public Token getToken() {
        char onget;
        while (true) {
            switch (onget = next()) {
                case EOF: {
                    return new Token("EOF", Token.TYPE_EOF, line, ++offset);
                }
                case '#': {
                    while (true) {
                        onget = next();
                        if (onget == '\n') {
                            line++;
                            break;
                        }
                        ++offset;
                    }
                }
                case '\n':
                    line++;
                    offset = 0;
                case '\r':
                case ' ':
                case '\t':
                    continue;
                case ';':
                    return new Token(";", Token.TYPE_BREAK, line, ++offset);
                default:
                    break;
            }

            String onStr = String.valueOf(onget);

            switch (onget) {
                case '{':
//                case '$':
                case '}':
                case ':':
                case '=': {
                    return new Token(onStr, Token.TYPE_OP, line, ++offset);
                }
            }

            //标识符
            while (true) {
                onget = next();
                switch (onget) {
                    case ';':
                    case '#':
                    case ' ':
                    case '\n':
                    case '\r':
                    case '\t':
                    case '{':
                    case '}': {
                        try {
                            inputStream.unread((int) onget);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return new Token(onStr, Token.TYPE_VAR, line, ++offset);
                    }
                    default: {
                        onStr += onget;
                    }

                }
            }


        }

    }

    public char next() {
        try {
            return (char) inputStream.read();
        } catch (IOException e) {
            e.printStackTrace();
            return (char) -1;
        }
    }


    public static void main(String[] args) {
        ConfParser parser = new ConfParser("/home/tjc/code/net-manager/nginx.conf");
        Token t = null;
        while (true) {
            t = parser.getToken();
            if(t.getType() == Token.TYPE_EOF) {
                break;
            }
            System.out.println(t.getValue());
        }
    }
}
